layui.define(function(exports){ //提示：模块也可以依赖其它模块，如：layui.define('layer', callback);
  var sysParamPage = {
    render: function(){
    	var $ = layui.$,
    	table = layui.table,
    	layer = layui.layer,
    	form = layui.form,
    	laydate = layui.laydate;
    	var optName;
    	var tableObj = table.render({
    	  id: 'main-table',
    	  elem: '#table', //指定原始表格元素选择器（推荐id选择器）
    	  url: '/boss/sys_param/query_list',
    	  method:'post',
    	  autoSort : false,
    	  size: 'sm',
		  even: true,
    	  toolbar: '#toolbar',
    	  height: 'full-70',
    	  limits : [10,30,50,100],
    	  limit: 30,
    	  page: true,
    	  cols: [[
    		  {type:'checkbox'},
    		  {field: 'paramName', title: '参数名',sort:false,templet: function(d){
    			  switch(d.paramName) {
    			  	case null:
    			  		return '空';
    			  	case '':
    			  		return '--';
    			  	default:
    			  		return d.paramName;
    			  }
    	      }},
    		  {field: 'paramValue', title: '参数值',sort:false,templet: function(d){
    			  switch(d.paramValue) {
    			  	case null:
    			  		return '空';
    			  	case '':
    			  		return '--';
    			  	default:
    			  		return d.paramValue;
    			  }
    	      }},
    		  {field: 'remark', title: '备注',sort:false,templet: function(d){
    			  switch(d.remark) {
    			  	case null:
    			  		return '空';
    			  	case '':
    			  		return '--';
    			  	default:
    			  		return d.remark;
    			  }
    	      }},
    		  {field: 'createTime', title: '创建时间',sort:false,templet: function(d){
    			  switch(d.createTime) {
    			  	case null:
    			  		return '空';
    			  	case '':
    			  		return '--';
    			  	default:
    			  		return d.createTime;
    			  }
    	      }},
    		  {field: 'updateTime', title: '更新时间',sort:false,templet: function(d){
    			  switch(d.updateTime) {
    			  	case null:
    			  		return '空';
    			  	case '':
    			  		return '--';
    			  	default:
    			  		return d.updateTime;
    			  }
    	      }},
    		  {fixed: 'right', title:'操作', toolbar: '#optbar', width:165}
    	  ]],
    	  parseData: function(res){
		  }
    	});
    	
    	/** 顶部toolbar事件 **/
    	table.on('toolbar(table)', function(obj){
    		switch(obj.event){
    		  case 'refresh-opt':
  			    tableObj.reload();
  			  break;
    		  case 'addData-opt':
    			  var data = obj.data;
    			  optName = 'new';
    			  layer.open({
    				  type: 1,
    				  area: ['700px','420px'],
    				  title : '新增/添加',
    				  content: $('#windows'),
    				  resize : false,
    				  move : false,
    				  shadeClose : false,
    				  btn : [ '确定', '取消' ],
    				  success : function(layero, index){
    					  $('#win-form')[0].reset();
    				  },
					  yes : function(index, layero) {
						  $('#win-submit').trigger('click');
					  }
    				});
		      break;
    		  case 'batch-del-opt':
    			  var checkStatus = table.checkStatus('main-table');
    			  if(checkStatus.data.length ==0){
    				  layer.alert('请先选择待删除行',{icon: 5});
    				  return;
    			  }
    			  layer.confirm("你确定批量删除"+checkStatus.data.length+"个项目吗？", {icon: 3, title:'提示'},
    			            function(index){//确定回调
        				  		var keys = new Array();
        				  		$.each(checkStatus.data,function(i,item){
        				  			keys.push(item.id);
        				  		});
        				  		batchDelete(keys,index);
    			            },function (index) {//取消回调
    			               layer.close(index);
    			            }
    			        );
    			  break;
    		}
    	});
    	
    	form.on('submit(win-submit)', function(data){
  		  $.ajax({
  			    type: 'POST',
  			    url: optName=='new'?'/boss/sys_param/add_item':'/boss/sys_param/update_item',
  			    data: data.field,
  			    success: function (data) {
  			        if(data.success){
  			        	layer.closeAll();
  			        	tableObj.reload();
  			        	layer.msg(data.message);
  			        }else{
  			            layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
  			        }
  			    },
  			    error: function(data) {
  			        layer.msg(data.responseJSON.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
  			    },
  			    dataType: "json"
  			});
  		  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
  		});
    	
    	/** 右侧tool事件 **/
    	table.on('tool(table)', function(obj){
    		var data = obj.data;
    		switch(obj.event){
    		  case 'edit-opt':
    			  optName = 'update';
    			  layer.open({
    				  type: 1,
    				  area: ['700px','420px'],
    				  title : '编辑',
    				  content: $('#windows'),
    				  shadeClose : false,
    				  move : false,
    				  btn : [ '确定', '取消' ],
    				  success : function(layero, index){
    					  $('#win-form')[0].reset();
    					  $.post(
    							  "/boss/sys_param/get_item_by_key",
    							  {id:data.id},
    							  function(data, textStatus, jqXHR){
    								  form.val("win-form",data);
    							  },"json");
    				  },
					  yes : function(index, layero) {
						  $('#win-submit').trigger('click');
					  }
    				});
    			  break;
    		  case 'del-opt':
    			  layer.confirm("你确定删除参数“"+obj.data.paramName+"”吗？", {icon: 3, title:'提示'},
  			            function(index){//确定回调
      				  		var keys = new Array();
      				  		keys.push(obj.data.id);
      				  		batchDelete(keys,index);
  			            },function (index) {//取消回调
  			               layer.close(index);
  			            }
  			        );
    			  break;
    		}
    	});
    	
    	form.on('submit(search-opt)',function(data){
    		tableObj.reload({where:data.field});
    	    return false;
    	});
    	
    	function batchDelete(keys,index){
    		$.ajax({
	                type: 'POST',
	                url: '/boss/sys_param/delete_item',
	                data: JSON.stringify(keys),
	                success: function (data) {
	                    if(data.success){
	                        layer.close(index);
	                    }else{
	                        layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
	                    }
	                    tableObj.reload();
	                    layer.msg(data.message);
	                },
	                error: function(data) {
	                    layer.msg(data.responseText,{icon: 5,anim: 6,title:'错误',time: 0,btn:['确定']});
	                },
	                contentType: "application/json",
	                dataType: "json"
	            });
    	}
    }
  };
  
  //输出test接口
  exports('sysParamPage', sysParamPage);
});